package com.hui.config;

import com.hui.handler.interceptor.JwtAuthenticationInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * 图片本地资源映射器
 */
@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {

    @Value("${file.path}")
    private String path;

    @Value("${file.avatar}")
    private String avatar;

    //注册拦截器token拦截器
    private final JwtAuthenticationInterceptor jwtAuthenticationInterceptor;

    /**
     * 例如有一个url：http://localhost:8081/images/Hello.jpg
     * <p>
     * 表示要对Hello.jpg进行请求访问，这时候服务器会把这个请求的资源映射到我们配置的路径之下 也就是会到 fileSavePath
     * 下去寻找 是否有 Hello.jpg 这个资源，返回给前端页面。
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/file/**").addResourceLocations("file:" + path);
        registry.addResourceHandler("/avatar/**").addResourceLocations("file:" + avatar);
        registry.addResourceHandler("/img/**").addResourceLocations("file:" + path);
        //接口文档资源
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/static/");
//        System.out.println("file:" + path);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //addPathPatterns 拦截路径 excludePathPatterns 排除路径
        registry.addInterceptor(jwtAuthenticationInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns("/api/index/**") //首页
                .excludePathPatterns("/api/login") //登录
                .excludePathPatterns("/api/register/**") //注册
                .excludePathPatterns("/api/category") //商品分类
                .excludePathPatterns("/doc.html")//文档地址
        ;
    }

    @Bean
    public Docket createRestApi() {
        // 文档类型
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hui.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("无忧")
                .version("1.0")
                .description("无忧接口文档")
                .build();
    }
}